package com.seeyon.bugfixtools.custom.datasource.interceptor;

import com.seeyon.bugfixtools.custom.datasource.manager.DynamicDataSourceConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cans
 * @date 2024/5/5
 **/
@Component
public class DatasourceInterceptor implements HandlerInterceptor {

    @Resource
    private DynamicDataSourceConfig dynamicDataSourceConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!dynamicDataSourceConfig.hasDatasource() && !request.getRequestURI().equals("/index.html")) {
            response.sendRedirect("/index.html"); // 重定向到新的URL
            return false; // 返回false停止后续处理
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

}
